from OAPro import db

class BaseModel(db.Model):
    __abstract__ = True
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)

    def save(self):
        session = db.session()
        session.add(self)
        session.commit()

    def update(self):
        session = db.session()
        session.commit()

    def delete(self):
        session = db.session()
        session.delete(self)
        session.commit()

class Account(BaseModel):
    """
    账号
    """
    __tablename__ = "account"
    username = db.Column(db.String(32), unique=True)
    password = db.Column(db.String(32))
    is_activate = db.Column(db.String(4)) #账号是否激活

    a_s = db.relationship(
        "Staff",
        backref = "s_a",
        uselist = False
    )

class Department(BaseModel):
    """
    部门
    """
    __tablename__ = "department"
    d_name = db.Column(db.String(32), unique=True)
    d_desc = db.Column(db.Text)

    d_s = db.relationship(
        "Staff",
        backref = "s_d"
    ) #部门员工

    d_p = db.relationship(
        "Position",
        backref = "p_d"
    ) #部门职位


class Position(BaseModel):
    """
    职位
    """
    __tablename__ = "position"
    p_name = db.Column(db.String(32), unique=True)
    p_desc = db.Column(db.Text)
    p_level = db.Column(db.Integer)

    p_department = db.Column(db.Integer,db.ForeignKey("department.id"))

    pos_s = db.relationship(
        "Staff",
        backref="s_pos"
    )  # 职位员工

class Staff(BaseModel):
    """
    职员
    """
    __tablename__ = "staff"
    nickname = db.Column(db.String(32))
    gender = db.Column(db.String(4))
    age = db.Column(db.Integer)
    nation = db.Column(db.String(32))
    education = db.Column(db.String(20))
    phone = db.Column(db.String(16))
    email = db.Column(db.String(32))
    photo = db.Column(db.String(32))
    join_time = db.Column(db.Date)

    s_position = db.Column(db.Integer,db.ForeignKey("position.id")) #员工职位
    s_department = db.Column(db.Integer,db.ForeignKey("department.id")) #员工部门
    s_account = db.Column(db.Integer,db.ForeignKey("account.id")) #员工登录

    s_l = db.relationship(
        "LeaveNote",
        backref = "l_s"
    ) #员工假条

    s_sa = db.relationship(
        "Salary",
        backref="sa_s"
    ) #员工薪资

    s_n = db.relationship(
        "Notice",
        backref="n_s"
    )  # 员工薪资


staff_power = db.Table(
    'staff_power',
    db.Column('staff_id', db.Integer, db.ForeignKey("staff.id")),
    db.Column('power_id', db.Integer, db.ForeignKey("power.id"))
) #员工权限表

position_power = db.Table(
    'position_power',
    db.Column('position_id', db.Integer, db.ForeignKey("position.id")),
    db.Column('power_id', db.Integer, db.ForeignKey("power.id"))
) #员工权限表


class Power(BaseModel):
    """
    权限
    """
    __tablename__ = "power"
    power_name = db.Column(db.String(32))
    power_obj = db.Column(db.String(32))
    power_desc = db.Column(db.Text)

    p_pos = db.relationship(
        "Position",
        secondary = position_power,
        backref = "pos_p"
    ) #权限职位

    p_s = db.relationship(
        "Staff",
        secondary=staff_power,
        backref="s_p"
    )  # 权限职位


class Notice(BaseModel):
    """
    公告
    """
    __tablename__ = "notice"
    title = db.Column(db.String(32))
    n_type = db.Column(db.String(16))
    n_time = db.Column(db.Date)
    content = db.Column(db.Text)

    n_person = db.Column(db.Integer,db.ForeignKey("staff.id"))

class Salary(BaseModel):
    """
    薪资
    """
    __tablename__ = "salary"
    s_time = db.Column(db.Date)
    amount = db.Column(db.Float) #实发金额
    welfare_deduction = db.Column(db.Float) #福利扣除
    tax = db.Column(db.Float) #个税扣除
    violation = db.Column(db.Float) #违纪扣除
    total =  db.Column(db.Float) #总扣除
    subsidy =  db.Column(db.Float) #补助
    award = db.Column(db.Float) #奖金
    s_person = db.Column(db.Integer,db.ForeignKey("staff.id"))

class LeaveNote(BaseModel):
    """
    假条
    """
    __tablename__ = "leavenote"
    check_person_id = db.Column(db.Integer) #审核人id
    check_person = db.Column(db.String(32)) #审核人姓名
    leave_time = db.Column(db.Date)
    leave_type = db.Column(db.String(16))
    leave_reason = db.Column(db.Text)
    leave_long = db.Column(db.Float)
    leave_statue = db.Column(db.String(16)) #假条状态  申请中 通过  驳回

    l_person = db.Column(db.Integer,db.ForeignKey("staff.id"))